package com.cnsoft.player.game;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cnsoft.framework.beans.common.RESTResultBean;
import org.cnsoft.framework.beans.user.LoginerBean;
import org.cnsoft.framework.beans.user.UserBean;
import org.cnsoft.framework.support.MyServiceSupport;
import org.cnsoft.framework.token.TokenServiceImpl;
import org.cnsoft.framework.utils.EmptyHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.cnsoft.persistent.player.U101111PlayerPassport.U101111PlayerPassportDBO;
import com.cnsoft.persistent.player.U101111PlayerPassport.U101111PlayerPassportDao;

/**
 * 超管登录
 * 
 * @author ZmSoft
 * @version 0.1.0 2018/3/1
 * @since 0.1.0 2018/3/1
 * @see ApiManagerLoginController
 */
@Service("PlayerLoginService") // 替换系统登录接口
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class GamePlayerLoginService extends MyServiceSupport {
	@Resource
	private U101111PlayerPassportDao daoU101111PlayerPassport;

	public RESTResultBean<UserBean> doLogin(HttpServletRequest request, HttpServletResponse response, LoginerBean loginer) throws Exception {
		logger.debug("UserBean==>>>" + loginer.toJsonString());
		RESTResultBean<UserBean> result = new RESTResultBean<UserBean>();

		// 参数校验
		if(super.checkSecurityCode(request, response)==false) {
			result.setCode(1);
			result.setMsg("验证码失败");
		} else if (EmptyHelper.isEmpty(loginer.getAccount())) {
			result.setCode(1);
			result.setMsg("登录账户不能为空");
		} else if (EmptyHelper.isEmpty(loginer.getPassWord())) {
			result.setCode(1);
			result.setMsg("密码不能为空");
		} else {
			U101111PlayerPassportDBO playerPassportDBO = new U101111PlayerPassportDBO();
			// s9010UserDBO.setAccountType(accounttype);//1用户名2手机3邮箱
			playerPassportDBO.setUserAccount(loginer.getAccount());
			// _SystemManagerDBO_.setHdp(loginer.getHdp());//区分用户渠道类别
			//playerPassportDBO.prepareDynamicSql("creator = 'SYSTEM'");

			List<U101111PlayerPassportDBO> users = daoU101111PlayerPassport.doSelectData(playerPassportDBO);
			if (users == null) {
				result.setCode(1);
				result.setMsg("用户不存在");
			} else if (users.size() == 0) {
				result.setCode(1);
				result.setMsg("用户不存在");
			} else if (users.size() > 1) {
				result.setCode(2);
				result.setMsg("账户异常");
			} else {
				playerPassportDBO = users.get(0);// 获得 用户数据
				// PasswordHelper.getPassword(loginer.getPassWord())
				if (playerPassportDBO.getUserPassword().equals(loginer.getPassWord())) {
					playerPassportDBO.setUserPassword(null);// 保证安全
					UserBean curUser = new UserBean(playerPassportDBO.getId());
					BeanUtils.copyProperties(playerPassportDBO, curUser);
					curUser.setUserName(playerPassportDBO.getNickName());

					// 保存系统类别
					curUser.setSid(playerPassportDBO.getSid());
					curUser.setIsAdmin(ONE);////////////////////////////////////////////////////////

					// 缓存中心缓存用户对象
					// 制作Token
					TokenServiceImpl tokenService = TokenServiceImpl.build(request, curUser);
					tokenService.setCacheService(myCacheService);
					tokenService.prepareClientTimestamp();

					tokenService.setWaitTimeSecond(1800);
					tokenService.saveToken(request);// 处理TOKEN

					// 记录登录信息
					{
//						playerPassportDBO.setId(PKHelper.creatPUKey());
//						playerPassportDBO.setLoginStatus(ZERO);
//						playerPassportDBO.setOnlineType(TWO);
//						// _SystemManagerDBO_.setOnlineLastIp(tokenBean.getIpAdress());
//						playerPassportDBO.setOnlineLastTime(DateHelper.currentTimeMillisCN1());
//						_SystemManagerService_.saveLoginLog(playerPassportDBO);
					}

					result.setData(tokenService);

				} else {
					result.setCode(3);
					result.setMsg("密码错误");
				}
			}
		}
		logger.debug("RESTResultBean==>>>" + result.toJsonString());
		return result;
	}

	/**
	 * 获得一个临时Token
	 * @param request
	 * @param response
	 * @param sid
	 * @param gameid
	 * @return
	 */
	public RESTResultBean<UserBean> doLoading(HttpServletRequest request, HttpServletResponse response, String sid, LoginerBean gameid) {
		RESTResultBean<UserBean> result = new RESTResultBean<UserBean>();
		
		result.setToken(request.getSession().getId());
		
		return result;
	}
}
